import { AbstractMesh } from "@babylonjs/core/Meshes/abstractMesh";
import { DefaultStage } from "../default-stage";
import { RawAssetsUrl } from "../../raw-assets-info";
import { InstancedMesh } from "@babylonjs/core/Meshes/instancedMesh";
import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";
import { StandardMaterial } from "@babylonjs/core/Materials/standardMaterial";
import { Mesh } from "@babylonjs/core/Meshes/mesh";
import { Quaternion, Vector3 } from "@babylonjs/core/Maths/math.vector";
import { Random } from "babylon-lib/tool/random";
import { Axis, Color3, Material } from "@babylonjs/core";
import { RenderOrder } from "babylon-lib/misc/render-order";
import * as Data from '../../data/data';


class ExplodeImprint {

    private static _mark: AbstractMesh;
    private static _marks: InstancedMesh[] = [];

    public static initialize(stage: DefaultStage): void {
        this.initializeMark(stage);
    }

    public static generate(position: Vector3): void {
        const inm = (this._mark as Mesh).createInstance('explode-mark-mesh-' + this._marks.length);
        inm.position.copyFrom(position);
        const s = Random.float(0.8, 1.2);
        inm.scaling.set(s, s, s);
        const r = Random.float(0, 6.28);
        inm.rotationQuaternion = Quaternion.FromEulerAngles(0, r, 0);
        inm.renderingGroupId=RenderOrder.id(RenderOrder.baseName);
        this._marks.push(inm);
    }

    private static initializeMark(stage: DefaultStage): void {
        const texture = stage.loader(stage.mainScene).getTextureTaskInfo(RawAssetsUrl.textureMark)?.texture!;
        const mesh = MeshBuilder.CreatePlane('explode-mark-mesh', { size: 10 }, stage.mainScene);
        mesh.rotate(Axis.X, 1.5708);
        mesh.bakeCurrentTransformIntoVertices();
        const material = new StandardMaterial('explode-mark-material', stage.mainScene);
        material.opacityTexture=texture;
        material.disableLighting = true;
        material.needAlphaTesting();
        material.needAlphaBlending();
        material.alphaCutOff = 0.1;
        material.transparencyMode=StandardMaterial.MATERIAL_ALPHATEST;
        material.transparencyMode=StandardMaterial.MATERIAL_ALPHABLEND;
        mesh.material = material;
        mesh.isVisible = false;
        this._mark = mesh;
    }

}


export { ExplodeImprint };